// 引入媒体查询模块
import mediaquery from '@ohos.mediaquery';

// 创建storage实例
export let storage = new LocalStorage({breakPoint: 'sm'});

export class BreakPointSystem{
  //定义监听句柄
  listenerSM = mediaquery.matchMediaSync('(320vp<=width<520vp)');
  listenerMD = mediaquery.matchMediaSync('(520vp<=width<840vp)');
  listenerLG = mediaquery.matchMediaSync('(840vp<=width)');

  //设置AppStorage方法
  updateCurrentBreakPoint(breakPoint: string){
    //设置
    storage.set('breakPoint', breakPoint)
  }

  //sm事件函数
  smListenerCallback(e){
    if(e.matches){
      //把断点存储到 localStorage 中
      this.updateCurrentBreakPoint('sm');
      // console.log('smmm')
    }
  }

  mdListenerCallback(e){
    if(e.matches){
      //把断点存储到 localStorage 中
      this.updateCurrentBreakPoint('md');
      // console.log('mddd')

    }
  }

  lgListenerCallback(e){
    if(e.matches){
      //把断点存储到 localStorage 中
      this.updateCurrentBreakPoint('lg');
      // console.log('lggg')

    }
  }

  //注册事件绑定
  register(){
    this.listenerSM.on('change', this.smListenerCallback.bind(this));
    this.listenerMD.on('change', this.mdListenerCallback.bind(this));
    this.listenerLG.on('change', this.lgListenerCallback.bind(this));
  }

  //注销事件绑定
  unregister(){
    this.listenerSM.off('change', this.smListenerCallback.bind(this));
    this.listenerMD.off('change', this.mdListenerCallback.bind(this));
    this.listenerLG.off('change', this.lgListenerCallback.bind(this));
  }

}